package org.csu.jpetstore_users_backend_pj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.jpetstore_users_backend_pj.entity.Account;
import org.csu.jpetstore_users_backend_pj.mapper.AccountMapper;
import org.csu.jpetstore_users_backend_pj.service.UserService;
import org.csu.jpetstore_users_backend_pj.util.EncryptUtil;
import org.csu.jpetstore_users_backend_pj.vo.AccountVO;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userService")
@MapperScan("org.csu.petstore.mapper")
public class UserServiceImpl extends ServiceImpl<AccountMapper,Account> implements UserService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private EncryptUtil encryptUtil;
    @Override
    public Account getUserByGiteeId(String giteeId) {
        return this.getOne(new QueryWrapper<Account>().eq("gitee_id", giteeId));
    }

//    @Override
//    public void bindGiteeAccount(String username, Account giteeAccount) {
//        Account account = this.getById(username);
//        if (account != null) {
//            account.setGiteeId(giteeAccount.getGiteeId());
//            account.setGiteeAvatar(giteeAccount.getGiteeAvatar());
//            account.setGiteeNickname(giteeAccount.getGiteeNickname());
//            account.setGiteeToken(giteeAccount.getGiteeToken());
//            this.updateById(account);
//        }
//    }

    @Override
    public AccountVO login(String username,String password){

        Account account = accountMapper.getAccountByUsername(username);
        if (account == null) {
            return null;
        }
        if(!encryptUtil.verifyPwd(password, accountMapper.getPwdHashByUsername(username))){
            System.out.println("222");
            return null;
        }
        AccountVO accountVO = new AccountVO();
        accountVO.setUsername(account.getUserid());
        accountVO.setEmail(account.getEmail());
        accountVO.setFirstName(account.getFirstName());
        accountVO.setLastName(account.getLastName());
        accountVO.setStatus(account.getStatus());
        accountVO.setAddress1(account.getAddress1());
        accountVO.setAddress2(account.getAddress2());
        accountVO.setCity(account.getCity());
        accountVO.setState(account.getState());
        accountVO.setZip(account.getZip());
        accountVO.setCountry(account.getCountry());
        accountVO.setPhone(account.getPhone());
        return accountVO;
    }
    public AccountVO loginByPhone(String phone) {
        // 假设根据 phone 查询用户（需实现数据库逻辑）
        AccountVO account = new AccountVO();
        account.setUsername(phone); // 简化示例
        // 从数据库查询真实用户
        return account; // 返回 null 如果未找到
    }

    @Override
    public void register(AccountVO account, String password) {
        //判断是通过gitee直接登录后再注册还是普通注册
        //交给全局异常处理器处理
        accountMapper.insertSignon(account.getUsername(), encryptUtil.encryptPwd(password));
        accountMapper.insertAccount(account);
        accountMapper.insertProfile(account);
    }

    @Override
    public boolean IsUsernameExist(String username) {
        if(accountMapper.getAccountByUsername(username)!=null||username.equals("")){
            return true;
        }
        return false;
    }

    @Override
    public boolean IsEmailExist(String email) {
        if(accountMapper.getAccountByEmail(email)!=null){
            return true;
        }
        return false;
    }

    @Override
    public boolean IsPhoneExist(String phone) {
        if(accountMapper.getAccountByPhone(phone)!=null){
            return true;
        }
        return false;
    }
}

